package com.jin.flashsaled.utils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;

public class UserUtil {

    // 目标文件路径，可改成你想存放的任意位置
    private static final Path FILE = Paths.get("data.txt");

    public static void main(String[] args) {
        // 模拟 10 组数据
        for (int i = 1; i <= 10; i++) {
            String varA = "A" + i;
            String varB = "B" + i;
            appendLine(varA, varB);
        }
        System.out.println("写入完成！");
    }

    /**
     * 把两个变量用逗号拼接后追加到文件末尾
     */
    public static void appendLine(String val1, String val2) {
        // 如果文件不存在，会自动创建；StandardOpenOption.CREATE 保证首次运行不报错
        try (BufferedWriter writer = Files.newBufferedWriter(
                FILE, StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
            writer.write(val1 + "," + val2);
            writer.newLine();   // 换行
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** 生成 count 个不重复手机号，装进 List 返回 */
    public static List<String> genUserName(int count) {
        // 用于去重
        Set<Integer> seen = new HashSet<>(count * 2);
        Random rand = new Random();
        List<String> list = new ArrayList<>(count);

        while (list.size() < count) {
            int suffix = rand.nextInt(900_000_000); // 0~899 999 999
            if (seen.add(suffix)) {                 // 去重成功
                long phone = 13_000_000_000L + suffix;
                list.add(Long.toString(phone));
            }
        }
        return list;
    }
}